// MQTT 发送指令前的编码

function decToHex(decNumber) {
    let str = Number(decNumber).toString(16).padStart(2, '0');
    return str
}

function waterMessageDecode(str){
    if (!str) {
        console.warn('喂水器信息编码,原始字符串不得为空')
        return;
    }
    let num = 0
    for (let i = 0; i < str.length; i += 2) {
        let decimalNumber = parseInt(`${str[i]}${str[i + 1]}`, 16);
        num += decimalNumber
    }
    num = num - 256
    let code = decToHex(num)
    let newStr = `${str}${code}ab`
    return newStr
}

console.log( waterMessageDecode('55aaa40425520000') )
